﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Itop.KSRY.Model;
using DevExpress.Utils;
using System.Collections;
using System.Threading;

namespace Itop.KSRY {
    public partial class UCksmanKS : UserControl {
        public UCksmanKS() {
            InitializeComponent();
            barButtonItem1.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItem1_ItemClick);
            barButtonItem2.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItem2_ItemClick);
        }

        void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            //导出
            if(DateTime.Now<new DateTime(2012,5,1))
            export();
        }

        void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            //抽考
            exam();
        }
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            colksbz.Visible = false;
            coljb.Visible = false;
        }

        private void exam() {
            sysset set = DBHelper.SqlMapper.GetOneByKey<sysset>("0");
            if (set == null)
                MessageBox.Show("请先维护抽考参数");
            WaitDialogForm waitdlg =new WaitDialogForm("","开始选取人员,请稍候。。。");
            IList<ksman> list = DBHelper.SqlMapper.GetList<ksman>(null);
            DataTable dt = ConvertHelper.ToDataTable((IList)list, typeof(ksman));
            IList dwlist = DBHelper.SqlMapper.GetList("SelectOneStr", "select distinct dwname from ksman");
            //waitdlg.SetCaption(dwlist.Count + "");
            manlist.Clear();
            manlist = new List<ksman>();
            string wh1 = "ksbz<>'否' and ";
            if (set.nl > 0)
                wh1 += " nl<" + set.nl + " and ";
            if(set.dj0>0)
            manlist.AddRange( select(dt.Select(wh1+"jb=0"), set.dj0));
            if(set.dj1>0)
            manlist.AddRange( select(dt.Select(wh1+"jb=1"), set.dj1));
            if(set.dj2>0)
            manlist.AddRange( select(dt.Select(wh1+"jb=2"), set.dj2));
            if (set.dj3 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=3 and dwname='" + dw + "'"), set.dj3));
            }
            if (set.dj4 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=4 and dwname='" + dw + "'"), set.dj4));
            }
            if (set.dj5 > 0) {
                foreach (string dw in dwlist)
                    manlist.AddRange(select(dt.Select(wh1+"jb=5 and dwname='" + dw + "'"), set.dj5));
            }
            int i=1;
            foreach (ksman item in manlist) {
                item.orderid = i++;
            }

            ksmanBindingSource.DataSource = manlist;
            //Thread.Sleep(1000);
            waitdlg.Close();
        }
        List<ksman> manlist = new List<ksman>();
        private IList<ksman> select(DataRow[] rows, int num) {
            IList<ksman> list = new List<ksman>();
            if (num >= rows.Length) {
                foreach (DataRow row in rows) {
                    list.Add(ConvertHelper.RowToObject<ksman>(row));
                }
            } else {
                ArrayList rowlist = new ArrayList(rows);
                int listnum = rowlist.Count;
                Random random=new Random();
                while (num > 0) {
                    DataRow row = rowlist[random.Next(1,listnum--) - 1] as DataRow;
                    rowlist.Remove(row);
                    num--;
                    list.Add(ConvertHelper.RowToObject<ksman>(row));
                }
            }
            return list;
        }
        private void export() {
            if (manlist.Count == 0) return;
            WaitDialogForm waitdlg = new WaitDialogForm("", "正在导出数据,请稍候。。。");
            DBHelper.ExportExcel(manlist);
            waitdlg.Close();
        }
    }
}
